var express = require('express');
var router = express.Router();
var { comment_model, qfuser_model, qfanno_model, station_model, part_model, qfannoxm_model, staff_model } = require("../utils/model")
var { createToken, checkToken } = require("../utils/token")
router.get("/test", (req, res) => {
    res.json({
        code: 200,
        msg: " test - 测试 api 路由接口是否有效 "
    })
})

router.post("/getdata", (req, res) => {
    var body = req.body;
    res.json({
        code: 200,
        body,
        msg: " test - 自己封装的 post 接口是否有效  "
    })
})

router.get("/commentList", (req, res) => {
    comment_model.find().then(result => {
        res.json({
            code: 200,
            msg: "数据请求成功",
            result
        })
    }).catch(err => {
        res.json({
            code: 500,
            msg: "服务器异常",
            err
        })
    })
})

router.post("/commentAdd", (req, res) => {
    var body = req.body
    body.time = new Date()
    comment_model.insertMany(body).then(result => {
        res.json({
            code: 200,
            msg: "插入成功",
            result
        })
    }).catch(err => {
        res.json({
            code: 500,
            msg: "服务器异常",
            err
        })
    })
})

router.post("/commentDel", (req, res) => {
    var body = req.body
    body.time = new Date()
    comment_model.remove({
        _id: body._id
    }).then(result => {
        res.json({
            code: 200,
            msg: "删除成功",
            result
        })
    }).catch(err => {
        res.json({
            code: 500,
            msg: "服务器异常",
            err
        })
    })
})

router.post("/commentUpdata", (req, res) => {
    var body = req.body
    body.time = new Date()
    comment_model.updateMany({
        _id: body._id
    }, {
        $set: {
            content: body.content
        }
    }).then(result => {
        res.json({
            code: 200,
            msg: "修改成功",
            result
        })
    }).catch(err => {
        res.json({
            code: 500,
            msg: "服务器异常",
            err
        })
    })
})

//注册
router.post("/register", (req, res) => {
    var body = req.body
    qfuser_model.findOne({
        phone: body.phone
    })
        .then(result => {
            if (result) {
                res.json({
                    code: 401,
                    msg: "当前的手机号码已经被注册",
                    result
                })
            } else {
                body.time = new Date();
                qfuser_model.insertMany(body)
                    .then(data => {
                        res.json({
                            code: 200,
                            msg: "成功",
                            result: data
                        })
                    })
            }
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})
// 登录
router.post("/login", (req, res) => {
    var body = req.body
    qfuser_model.findOne({
        account: body.account,
    })
        .then(result => {
            if (result) {
                if (result.password == body.password) {
                    // token 
                    body.phone = result.phone;    // 重要
                    var token = createToken(body)   // account  phone
                    res.json({
                        code: 200,
                        msg: "登录成功",
                        result,
                        token
                    })
                } else {
                    res.json({
                        code: 401,
                        msg: "登录失败",
                        result
                    })
                }
            } else {
                res.json({
                    code: 401,
                    msg: "当前用户未注册,请联系管理员或者注册",
                    result
                })
            }
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

//获取and校验验证码
//获取验证码
router.post("/sendcaptcha", (req, res) => {
    var body = req.body
    qfuser_model.findOne({
        phone: body.phone,
    })
        .then(result => {
            if (result) {
                // 发送验证码
                axios.get("http://47.104.209.44:3333/captcha/sent", {
                    params: {
                        phone: body.phone
                    }
                })
                    .then(value => {
                        var { data } = value;
                        if (data.code == 200) {
                            res.json({
                                code: 200,
                                msg: '发送成功',
                                data
                            })
                        } else {
                            res.json({
                                code: 401,
                                msg: '发送失败',
                                data,
                            })
                        }
                    })
            } else {
                res.json({
                    code: 401,
                    msg: "当前用户未注册,请联系管理员或者注册",
                    result
                })
            }
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})


// 校验验证码 
router.post("/checkcaptcha", (req, res) => {
    var body = req.body
    qfuser_model.findOne({
        phone: body.phone,
    })
        .then(result => {
            if (result) {
                axios.get("http://47.104.209.44:3333/captcha/verify", {
                    params: {
                        phone: body.phone,
                        captcha: body.captcha
                    }
                })
                    .then(value => {
                        var { data } = value;
                        if (data.code == 200) {
                            // token 
                            var token = createToken(body)   // phone
                            res.json({
                                code: 200,
                                msg: '校验验证码成功',
                                data,
                                token,
                                result
                            })
                        } else {
                            res.json({
                                code: 401,
                                msg: '校验验证码失败',
                                data,
                            })
                        }
                    })
            } else {
                res.json({
                    code: 401,
                    msg: "当前用户未注册,请联系管理员或者注册",
                    result
                })
            }
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

// 获取用户信息
router.get("/getuserinfo", (req, res) => {
    checkToken(req, res, ({ phone }) => {
        qfuser_model.findOne({
            phone
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户信息成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

// 上传文件
var multer = require("multer")
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './public/avatar')
    },
    filename: function (req, file, cb) {
        cb(null, 'WH2105-' + Date.now() + file.originalname)
    }
})

var upload = multer({ storage: storage }).any()
router.post("/uploadavatar", upload, (req, res) => {
    console.log(req.files)

    var path = req.files[0].path
    checkToken(req, res, ({ phone }) => {
        qfuser_model.updateMany({
            phone
        }, {
            $set: {
                avatar: path
            }
        }).then(result => {
            res.json({
                code: 200,
                msg: "上传成功",
                path: path,
                result
            })
        })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

router.post("/uploadimg", upload, (req, res) => {
    var path = req.files[0].path
    checkToken(req, res, ({ phone }) => {
        res.json({
            code: 200,
            msg: "上传成功",
            path: path,
        })
    })
})

// 获取用户列表
router.get("/getuserlist", (req, res) => {
    // phone 
    var {
        keyword,
        role
    } = req.query;
    var obj = {}
    if (keyword) {
        obj = {
            $or: [
                {
                    phone: new RegExp(keyword)
                },
                {
                    account: new RegExp(keyword)
                }
            ]
        }
    }

    if (role) {
        obj.role = role * 1;
    }
    checkToken(req, res, ({ phone }) => {   // token  校验成功 
        qfuser_model.find(obj)
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户列表成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//删除学员
router.post("/deluserone", (req, res) => {
    var body = req.body
    checkToken(req, res, ({ phone }) => {
        qfuser_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})


//添加公告
router.post("/addanno", (req, res) => {
    var body = req.body
    console.log(body);
    body.time = new Date();
    qfanno_model.insertMany(body)
        .then(data => {
            res.json({
                code: 200,
                msg: "成功",
                result: data
            })
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

//获取公告列表
router.get("/getannolist", (req, res) => {
    // phone 
    var {
        keyword,
        type
    } = req.query;
    var obj = {}
    if (keyword) {
        obj = {
            $or: [
                {
                    title: new RegExp(keyword)
                },
                {
                    author: new RegExp(keyword)
                }
            ]
        }
    }

    if (type) {
        obj.type = type;
    }
    checkToken(req, res, ({ phone }) => {   // token  校验成功 
        qfanno_model.find(obj)
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取公告列表成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//获取公告详情
router.get("/getannodet", (req, res) => {
    checkToken(req, res, ({ phone }) => {
        qfanno_model.findOne({
            _id: req.query._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户信息成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//修改公告
router.post("/updateanno", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        qfanno_model.updateMany({
            _id: body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//删除公告
router.post("/delannoone", (req, res) => {
    var body = req.body
    checkToken(req, res, ({ phone }) => {
        qfanno_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})



//添加岗位
router.post("/addstation", (req, res) => {
    var body = req.body
    console.log(body);
    body.time = new Date();
    station_model.find({
        name: body.name
    })
        .then(adata => {
            if (adata.length == 0) {
                console.log(adata);
                station_model.insertMany(body)
                    .then(data => {
                        res.json({
                            code: 200,
                            msg: "成功",
                            result: data
                        })
                    })
                    .catch(err => {
                        console.log(err)
                        res.json({
                            code: 500,
                            msg: "服务器异常",
                            err
                        })
                    })
            } else {
                res.json({
                    code: 401,
                    msg: "该岗位已存在",
                })
            }
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

// 获取岗位列表
router.get("/getstationlist", (req, res) => {
    checkToken(req, res, ({ phone }) => {   // token  校验成功 
        station_model.find()
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取岗位列表成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})
// 获取岗位列表
router.get("/getstations", (req, res) => {
    var {
        keyword,
        part
    } = req.query;
    console.log(keyword);
    var obj = {}
    if (keyword) {
        obj = {
            $or: [
                {
                    name: new RegExp(keyword)
                },
                {
                    desc: new RegExp(keyword)
                }
            ]
        }
    }

    if (part) {
        obj.part = part;
    }
    checkToken(req, res, ({ phone }) => {   // token  校验成功 
        station_model.find(obj)
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取岗位列表成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//删除岗位
router.post("/delstationone", (req, res) => {
    var body = req.body
    checkToken(req, res, ({ phone }) => {
        station_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//修改岗位
router.post("/updatestation", (req, res) => {
    var body = req.body;
    console.log(body);
    checkToken(req, res, ({ phone }) => {
        station_model.updateMany({
            _id: body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//添加部门
router.post("/addpart", (req, res) => {
    var body = req.body
    console.log(body);
    body.time = new Date();
    part_model.find({
        $or: [
            {
                pname: body.pname
            },
            {
                num: body.num
            }
        ]
    })
        .then(adata => {
            console.log(adata);
            if (adata.length == 0) {
                part_model.insertMany(body)
                    .then(data => {
                        res.json({
                            code: 200,
                            msg: "成功",
                            result: data
                        })
                    })
                    .catch(err => {
                        console.log(err)
                        res.json({
                            code: 500,
                            msg: "服务器异常",
                            err
                        })
                    })
            } else {
                res.json({
                    code: 401,
                    msg: "该部门已存在",
                })
            }
        }).catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

// 获取部门列表
router.get("/getpartlist", (req, res) => {
    checkToken(req, res, ({ phone }) => {   // token  校验成功 
        part_model.find()
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取部门列表成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//修改部门
router.post("/updatepart", (req, res) => {
    var body = req.body;
    console.log(body);
    checkToken(req, res, ({ phone }) => {
        part_model.updateMany({
            _id: body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//删除部门
router.post("/delpartone", (req, res) => {
    var body = req.body
    checkToken(req, res, ({ phone }) => {
        part_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

// 上传图片
router.post("/uploadxm", upload, (req, res) => {
    var path = req.files[0].path
    checkToken(req, res, ({ phone }) => {
        res.json({
            code: 200,
            msg: "上传成功",
            path: path,
        })
    })
})

//reactxm添加公告
router.post("/addannoxm", (req, res) => {
    var body = req.body
    console.log(body);
    body.time = new Date();
    qfannoxm_model.insertMany(body)
        .then(data => {
            res.json({
                code: 200,
                msg: "成功",
                result: data
            })
        })
        .catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

//reactxm获取公告列表
router.get("/getanadlist", (req, res) => {
    // phone 
    var {
        keyword,
        type,
        _id
    } = req.query;
    console.log(_id);
    var obj = {}
    if (keyword) {
        obj = {
            $or: [
                {
                    title: new RegExp(keyword)
                },
                {
                    author: new RegExp(keyword)
                },
            ]
        }
    }
    if (_id) {
        obj = {
            _id
        }
    }

    if (type) {
        obj.type = type;
    }
    checkToken(req, res, ({ phone }) => {   // token  校验成功 
        qfannoxm_model.find(obj)
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取公告列表成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//reactxm获取公告详情
router.get("/getanaddet", (req, res) => {
    checkToken(req, res, ({ phone }) => {
        qfannoxm_model.findOne({
            _id: req.query._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取用户信息成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//reactxm修改公告
router.post("/updateanad", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        qfannoxm_model.updateMany({
            _id: body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//reactxm删除公告
router.post("/delanadone", (req, res) => {
    var body = req.body
    checkToken(req, res, ({ phone }) => {
        qfannoxm_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

// 修改用户信息
router.post("/updatauserinfo", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        qfuser_model.updateMany({
            _id: body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//添加员工
router.post("/addstaff", (req, res) => {
    var body = req.body
    console.log(body);
    body.time = new Date();
    staff_model.find({
        $or: [
            {
                account: body.account
            },
            {
                phone: body.phone
            }
        ]
    })
        .then(adata => {
            console.log(adata);
            if (adata.length == 0) {
                staff_model.insertMany(body)
                    .then(data => {
                        res.json({
                            code: 200,
                            msg: "成功",
                            result: data
                        })
                    })
                    .catch(err => {
                        console.log(err)
                        res.json({
                            code: 500,
                            msg: "服务器异常",
                            err
                        })
                    })
            } else {
                res.json({
                    code: 401,
                    msg: "该员工已存在",
                })
            }
        }).catch(err => {
            console.log(err)
            res.json({
                code: 500,
                msg: "服务器异常",
                err
            })
        })
})

//获取员工信息
router.get("/getstafflist", (req, res) => {
    // phone 
    var {
        keyword,
        type,
        _id
    } = req.query;
    console.log(_id);
    var obj = {}
    if (keyword) {
        obj = {
            $or: [
                {
                    desc: new RegExp(keyword)
                },
                {
                    account: new RegExp(keyword)
                },
            ]
        }
    }
    if (_id) {
        obj = {
            _id
        }
    }

    if (type) {
        obj.part = type;
    }
    console.log(obj);
    checkToken(req, res, ({ phone }) => {   // token  校验成功 
        staff_model.find(obj)
            .then(result => {
                res.json({
                    code: 200,
                    msg: "获取公告列表成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//修改员工信息
router.post("/updatestaff", (req, res) => {
    var body = req.body;
    checkToken(req, res, ({ phone }) => {
        staff_model.updateMany({
            _id: body._id
        }, {
            $set: body
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "修改成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})

//删除员工信息
router.post("/delstaffone", (req, res) => {
    var body = req.body
    checkToken(req, res, ({ phone }) => {
        staff_model.remove({
            _id: body._id
        })
            .then(result => {
                res.json({
                    code: 200,
                    msg: "删除成功",
                    result
                })
            })
            .catch(err => {
                console.log(err)
                res.json({
                    code: 500,
                    msg: "服务器异常",
                    err
                })
            })
    })
})




module.exports = router